<template>
  <div class="dashboard-container" v-loading="loading">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部结构 -->
        <tree-node :treeNode="conpony" :is-root="true" @addDept="addDept" />
        <!-- 树形结构 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-node
            slot-scope="{ data }"
            :treeNode="data"
            @delpots="getDepartment"
            @addDept="addDept"
            @editDept="editDept"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartment"
    />
  </div>
</template>

<script>
import treeNode from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils/index'
export default {
  data () {
    return {
      conpony: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      node: null, //记录当前点击的节点
      loading: false
    }
  },
  components: {
    treeNode,
    addDept
  },
  created () {
    this.getDepartment()
  },

  methods: {
    async getDepartment () {
      this.loading = true
      let res = await getDepartment()
      // console.log(res)
      this.conpony = { name: res.data.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.data.depts, '')
      this.loading = false
    },

    // 添加部门
    addDept (node) {
      this.showDialog = true
      this.node = node
    },
    editDept (node) {
      this.showDialog = true
      this.node = node
      // console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style></style>
